package com.hongxun.project.assets.bModel.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongxun.project.assets.bModel.mapper.BModelMapper;
import com.hongxun.project.assets.bModel.domain.BModel;
import com.hongxun.project.assets.bModel.service.IBModelService;
import com.hongxun.common.utils.text.Convert;

/**
 * 模块 服务层实现
 *
 * @author hongxun
 * @date 2019-07-10
 */
@Service
public class BModelServiceImpl implements IBModelService {
    @Autowired
    private BModelMapper bModelMapper;

    /**
     * 查询模块信息
     *
     * @param id 模块ID
     * @return 模块信息
     */
    @Override
    public BModel selectBModelById(String id) {
        return bModelMapper.selectBModelById(id);
    }

    /**
     * 查询模块列表
     *
     * @param bModel 模块信息
     * @return 模块集合
     */
    @Override
    public List<BModel> selectBModelList(BModel bModel) {
        return bModelMapper.selectBModelList(bModel);
    }

    /**
     * 新增模块
     *
     * @param bModel 模块信息
     * @return 结果
     */
    @Override
    public int insertBModel(BModel bModel) {
        bModel.setId(UUID.randomUUID().toString());
        return bModelMapper.insertBModel(bModel);
    }

    /**
     * 修改模块
     *
     * @param bModel 模块信息
     * @return 结果
     */
    @Override
    public int updateBModel(BModel bModel) {
        return bModelMapper.updateBModel(bModel);
    }

    /**
     * 删除模块对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBModelByIds(String ids) {
        return bModelMapper.deleteBModelByIds(Convert.toStrArray(ids));
    }

    @Override
    public void insertOrReplaceOrDelete(List<BModel> entities) {
        for (BModel b : entities) {
            if (b.getDatastate().equals("删除")) {
                bModelMapper.deleteBModelById(b.getId());
            } else {
                BModel model = bModelMapper.selectBModelById(b.getId());
                if (model != null) {
                    bModelMapper.updateBModel(b);
                } else {
                    bModelMapper.insertBModel(b);
                }
            }
        }
    }

}
